# socket服务端
import socket
import threading

# 创建套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定IP和端口   3306是mysql的端口 80是http的端口  21是ftp的端口   22是ssh的端口 23是telnet的端口 
server_socket.bind(('127.0.0.1', 1234))

# 监听
server_socket.listen(1234)

def recv_data(client):
    while True: 
        # 接收客户端发送的数据
        data = client_socket.recv(1024)
        print(data.decode('utf-8'))

        # 发送数据给客户端
        client_socket.send(f'服务端收到消息：{data}'.encode('utf-8'))

while True:
    # 线程阻塞  ，等待客户端连接
    client_socket, client_address = server_socket.accept()

    # 接收客户端发送的数
    thread = threading.Thread(target=recv_data, args=(client_socket,))
    thread.start()

